#!/bin/sh
#desc:Add rule requirement
#type:local
#package:odcontrol


operators="=:equal,!=:different,-gt:greater than,-lt:smaller than"


#TODO Make this list dynamic
vars="weekday:weekday,hour:hour,monthday:day of the month"
VALUES="@weekday,1:monday,2:tuesday,3:wednesday,4:thursday,5:friday,6:saturday,7:sunday"
VALUES="$VALUES,@hour,00.00,01.00,02.00,03.00,04.00,05.00,06.00,07.00,08.00,09.00,10.00,11.00,12.00,13.00,14.00,15.00,16.00,17.00,18.00,19.00,20.00,21.00,22.00,23.00"
VALUES="$VALUES,@monthday,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31"

# Is odcontrol properly installed + configured? 
if test -d /etc/opendomo/control; then
	vars="$vars,@ports:Ports"
	cd /etc/opendomo/control
	for i in `grep "way='in'" -r * | cut -f1 -d.`; do
		desc=`grep 'desc=' $i.info | cut -f2 -d=`
		pvalues=`grep 'values=' $i.info | cut -f2 -d=`
		if test -z "$desc"; then
			desc="$i"
		fi
		if test -z "$pvalues"; then
			#TODO consider analog/digital ports
			pvalues="on,off,10,20,30,40,50,60,70,80,90,100"
		fi
		vars="$vars,$i:$desc"
		VALUES="$VALUES,@$i,$pvalues"
	done
fi



FILE=$1

if test -z "$FILE"; then
	echo "# Missing param"
else
	echo "#> Add requirement"
	echo "form:manageRules.sh	rowform"
	echo "	rule	rule	hidden	$FILE"
	echo "	action	action	hidden	addcond"
	echo "	variable	Variable	list[$vars]"
	echo "	condition	Condition	list[$operators]"
	echo "	value	Value	list[$VALUES]"
	echo "actions:"
	echo "	manageRules.sh	Add"
fi
echo
	
